﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;

namespace BarManager.Core.Entity
{
    [DataContract]
    public class Envio
    {
        [Required]
        [DataMember]
        public long Id { get; set; }

        [Required]
        [DataMember]
        public int ClienteId { get; set; }

        [Required]
        [DataMember]
        public int DomicilioId { get; set; }

        [Required]
        [DataMember]
        public DateTime FechaCreacion { get; set; }

        [Required]
        [DataType(DataType.Time)]
        [Display(Name = "Hora de Envio")]
        [DataMember]
        public DateTime HoraEnvio { get; set; }

        [Required]
        [DataMember]
        public EstadoEnvio Estado { get; set; }

        [Required]
        [RegularExpression(@"^([0-9]*\,?[0-9]+|[0-9]+\,?[0-9]*)?$",
            ErrorMessage = "Campo Cliente paga invalido (0,00)")]
        [DataType(DataType.Currency)]
        [Display(Name = "Cliente paga con")]
        [DataMember]
        public decimal? Paga { get; set; }

        [DataType(DataType.Currency)]
        [DataMember]
        public decimal? Vuelto { get { return Total == null ? 0 : Paga - Total; } }

        [Required]
        [RegularExpression(@"^([0-9]*\,?[0-9]+|[0-9]+\,?[0-9]*)?$",
            ErrorMessage = "Campo Recargo invalido (0,00)")]
        [DataType(DataType.Currency)]
        [DataMember]
        public decimal? Recargo { get; set; }        

        [DataMember]
        public decimal? Total
        {
            get { return Detalle == null ? 0 : Detalle.Sum(x => x.SubTotal) + (Recargo ?? 0); }
        }

        [DataMember]
        public virtual Domicilio Domicilio { get; set; }

        [DataMember]
        public virtual Cliente Cliente { get; set; }

        [DataMember]
        public virtual ICollection<EnvioDetalleBase> Detalle { get; set; }

        public Envio()
        {
            Estado = EstadoEnvio.Pendiente;
            FechaCreacion = DateTime.Now;
            HoraEnvio = DateTime.Now.AddMinutes(30);
        }
    }


    public enum EstadoEnvio : short
    {
        Pendiente = 1,
        Enviado = 2,
        Entregado = 3,
        Cancelado = 4
    }
}
